<?php
class login
{
	public function defaultaction()
	{
		echo view::show('/login/form', lib::getitem('POST', lib::NO_PERSISTENT_STORAGE));
	}
	
	public function process()
	{
		lib::setitem('POST', $_POST);
		
		if(!lib::checkseccode($_POST['seccode'])){
			lib::seterror(lang::WRONG_SECCODE);
			lib::sendto('/login');
		}
		
		$username = $_POST['username'];
		$password = $_POST['password'];
		
		if(empty($username)){
			lib::seterror(lang::REQUIRE_USERNAME);
			lib::sendto('/login');
		}
		
		if(empty($password)){
			lib::seterror(lang::REQUIRE_PASSWORD);
			lib::sendto('/login');
		}
		
		$user = new user(array('username'=>$username));
		if(auth::authenticate($user, $password)){
			lib::setitem('user', $user);
			lib::sendto();
		}else{
			lib::seterror(lang::INVAILD_USER);
			lib::sendto("/login");
		}
	}
}